package com.boyuan.aboutcase;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.boyuan.huijiaoyu.AddGroup;
import com.boyuan.huijiaoyu.R;
import com.boyuan.huijiaoyu.SwipeListViewForScrollView;
import com.example.contactfragment.ContactGroupService;
import com.example.contactfragment.ContactService;
import com.example.contactfragment.HomeContactAdapter;
import com.example.newfragment.DetailPerson;
import com.magicsoft.app.entity.CaseDetailEntity;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.helper.ToastHelper;
import com.magicsoft.app.wcf.CaseService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;
import com.magicsoft.app.wcf.listener.PostRecordResponseListener;
import com.magicsoft.constant.Constant;

public class NewCaseIndependentActivity extends Activity implements
		OnClickListener {

	private TextView mTv_jump_priority;
	private TextView mTv_jump_change_status;
	private EditText mEdi_case_title;
	private EditText mEdi_case_desc;
	private TextView mTv_case_type;
	private CaseService caseService;
	private String TAG = "NewCaseIndependentActivity";
	private ContactService contactService;
	private ArrayList<DetailPerson> datas = new ArrayList<DetailPerson>();
	private ArrayList<DetailPerson> addlist = new ArrayList<DetailPerson>();
	ArrayList<String> addnamelist = new ArrayList<String>();
	ArrayList<String> addphonelist = new ArrayList<String>();
	ArrayList<String> namelist = new ArrayList<String>();
	ArrayList<String> phonelist = new ArrayList<String>();
	private String id;
	private MyGridView gridview;
//	private MyAdapter adapter;
	private TextView casemember;
	private ContactGroupService contactgroupService;
	private String usermobile;
	String caseID;
	boolean isxianshi = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_independent);
		findViewById(R.id.rootview_newcase).setOnClickListener(this);
		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		id = sp.getString("id", "");
		usermobile = sp.getString("username", "");
//		getFrequentContactCache();
//		getFrequentContacts();
		initUI();
	}

	private void initUI() {
		findViewById(R.id.lay_back1).setOnClickListener(this);
		findViewById(R.id.lay_save_newcase).setOnClickListener(this);
		// findViewById(R.id.tv_addmembers).setOnClickListener(this);
		
		casemember = (TextView) findViewById(R.id.tv_casemember);
		casemember.setOnClickListener(this);

		mEdi_case_title = (EditText) findViewById(R.id.edi_case_title);
		mEdi_case_desc = (EditText) findViewById(R.id.edi_case_desc);
//		mTv_case_type = (TextView) findViewById(R.id.tv_case_type);

		mTv_jump_priority = (TextView) findViewById(R.id.tv_jump_priority);
		mTv_jump_change_status = (TextView) findViewById(R.id.tv_jump_change_status);
		mTv_jump_priority.setText("重要");
//		mTv_jump_priority.setBackgroundColor(Color.parseColor("#278785"));
		mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority3));
		mTv_jump_priority.setOnClickListener(this);
//		mTv_jump_change_status.setOnClickListener(this);
		
//		gridview = (MyGridView) findViewById(R.id.myGridView_newcase);
//		adapter = new MyAdapter();
//		gridview.setAdapter(adapter);
//		gridview.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
//					long arg3) {
//				CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
//				cb.toggle();
//			}
//		});
	}
	
//	class MyAdapter extends BaseAdapter{
//
//		@Override
//		public int getCount() {
//			return datas.size();
//		}
//
//		@Override
//		public Object getItem(int arg0) {
//			return null;
//		}
//
//		@Override
//		public long getItemId(int arg0) {
//			return 0;
//		}
//
//		@Override
//		public View getView(final int arg0, View arg1, ViewGroup arg2) {
//			View inflate = getLayoutInflater().inflate(R.layout.list_friend, null);
//			TextView tv = (TextView) inflate.findViewById(R.id.tvname);
//			tv.setText(namelist.get(arg0));
//			CheckBox cb = (CheckBox) inflate.findViewById(R.id.checkBox1);
//			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//				
//				@Override
//				public void onCheckedChanged(CompoundButton compoundButton, boolean ischeck) {
//					if(ischeck){
//						addlist.add(datas.get(arg0));
//						addnamelist.add(datas.get(arg0).getRealname());
//						addphonelist.add(datas.get(arg0).getMobile());
//					}else{
//						addlist.remove(datas.get(arg0));
//						addnamelist.remove(datas.get(arg0).getRealname());
//						addphonelist.remove(datas.get(arg0).getMobile());
//					}
//					if(addnamelist.size()>0){
//						casemember.setText(addnamelist.toString());
//					}else{
//						casemember.setText("");
//					}
//				}
//			});
//			return inflate;
//		}
//		
//	}
	
//	//获取常用联系人缓存
//	private void getFrequentContactCache() {
//		datas.clear();
//		namelist.clear();
//		phonelist.clear();
//		ArrayList<DetailPerson> frequentContactList = SharePreferenceHelper
//				.getContactFrindList(NewCaseIndependentActivity.this, id);
//		if (frequentContactList.size() > 0) {
//			datas.addAll(frequentContactList);
//			for (int i = 0; i < datas.size(); i++) {
//				namelist.add(datas.get(i).getRealname());
//				phonelist.add(datas.get(i).getMobile());
//			}
//		}
//	}
	
//	//获取常用联系人
//	private void getFrequentContacts() {
//
//		if (contactService == null) {
//			contactService = new ContactService(NewCaseIndependentActivity.this);
//		}
//		contactService.getFrequentContacts(id,
//				new GetOneRecordListener<List<DetailPerson>>() {
//
//					@Override
//					public void onFinish(List<DetailPerson> data) {
//						datas.clear();
//						namelist.clear();
//						phonelist.clear();
//						if (data != null && data.size() > 0) {
//							datas.addAll(data);
//							SharePreferenceHelper.saveContactFrindList(NewCaseIndependentActivity.this, id, data);
//							for (int i = 0; i < datas.size(); i++) {
//								namelist.add(datas.get(i).getRealname());
//								phonelist.add(datas.get(i).getMobile());
//							}
//						}
//						adapter.notifyDataSetChanged();
//					}
//
//					@Override
//					public void onFailed(String Message) {
//						ToastHelper.showMsg(NewCaseIndependentActivity.this, Message, false);
//					}
//				});
//	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_back1:
			finish();
			break;
		case R.id.lay_save_newcase:
			//新建case
//			if(addlist.size() > 0){
				Btn_commit();
//			}else{
//				ToastHelper.showMsg(NewCaseIndependentActivity.this, "请选择要添加的关注人", false);
//			}
			break;
		case R.id.tv_jump_priority:
			//选择优先级
			jump_priority();
			break;
		case R.id.tv_jump_change_status:
			//选择状态
			tv_jump_change_status();
			break;
//		case R.id.tv_casemember:
//			if(isxianshi == false){
//				gridview.setVisibility(View.GONE);
//				isxianshi = true;
//			}else{
//				gridview.setVisibility(View.VISIBLE);
//				isxianshi = false;
//			}
//			break;
		case R.id.rootview_newcase:
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			break;
		default:
			break;
		}
	}

	private String getStatusNum(String status) {
		if ("未处理".equals(status)) {
			return "1";
		} else if ("处理中".equals(status)) {
			return "2";
		} else if ("已关闭".equals(status)) {
			return "3";
		} else if ("等待客户确认".equals(status)) {
			return "4";
		} else if ("已完成".equals(status)) {
			return "5";
		}

		return "1";
	}

	private void Btn_commit() {
		final String title = mEdi_case_title.getText().toString().trim();
		final String desc = mEdi_case_desc.getText().toString().trim();
		final String priority = mTv_jump_priority.getText().toString().trim();
		final String status = mTv_jump_change_status.getText().toString()
				.trim();
		final String statusNum = getStatusNum(status);

		SharedPreferences sp = getSharedPreferences("a.xml", 0);
		final String uid = sp.getString("accountId", "");
		final String realname = sp.getString("realname", "");
		StringBuffer sb = new StringBuffer();
		sb.append("[");
//		for(int i=0;i<addlist.size();i++){
//			DetailPerson detailPerson = addlist.get(i);
//			String account_id = detailPerson.getAccount_id();
//			String realname2 = detailPerson.getRealname();
//			sb.append("{'id':" + "'" + account_id + "'" + ",'name':" + "'" + realname2 + "'" + "},");
//		}
//		sb.append("{'id':" + "'" + "4a4b67ce-983a-4de5-b819-ced4ab879fc4" + "'" + ",'name':" + "'" + "黄进强" + "'" + "},");
//		sb.append("{'id':" + "'" + "15b5f79d-ae1e-4acc-af7b-cdc6e17c826c" + "'" + ",'name':" + "'" + "陈章贤" + "'" + "},");
		String str = sb.toString();
//		int lastIndexOf = str.lastIndexOf(",");
//		str = str.substring(0, lastIndexOf);
		str = str + "]";
		str = str.replaceAll("\'", "\"");
		final String members1 = "";

		if (caseService == null) {
			caseService = new CaseService(this);
		}
//		caseService.getCaseType("1", new PostRecordResponseListener() {
//
//			@Override
//			public void onFinish(String succString) {//如果要加人把“”改成members1
				caseService.addCase(title, desc, setPriority(priority), uid, realname,
						new GetMultilParamsListener() {

							@Override
							public void onFinish(Object... params) {
								if(params != null){
									CaseNew caseNew = (CaseNew) params[0];
									caseID = caseNew.getCid();
									Intent data = new Intent();
									setResult(Constant.NewCaseDetail_Code, data);
									ToastHelper.showMsg(NewCaseIndependentActivity.this, "创建case成功！", false);
									finish();
								}
							}

							@Override
							public void onFailed(String failString) {
								ToastHelper.showMsg(
										NewCaseIndependentActivity.this, "无法请求服务！", false);
							}
						});
//			}
//
//			@Override
//			public void onFailed(String failString) {
//				ToastHelper.showMsg(NewCaseIndependentActivity.this,
//						failString, false);
//			}
//		});
	}
	
	public String setPriority(String str){
		String str1 = "";
		if(str.equals("非常紧急")){
			str1 = "1";
		}else if(str.equals("紧急")){
			str1 = "2";
		}else if(str.equals("重要")){
			str1 = "3";
		}else if(str.equals("一般")){
			str1 = "4";
		}else if(str.equals("不重要")){
			str1 = "5";
		}else if(str.equals("不紧急不重要")){
			str1 = "6";
		}
		return str1;
	}

	private void tv_jump_change_status() {
		Intent intent = new Intent();
		intent.setClass(this, CaseChangeStatusActivity.class);
		startActivityForResult(intent, Constant.CaseChangeStatus_CODE);
	}

	private void jump_priority() {
		Intent intent = new Intent();
		intent.setClass(this, CaseChangePriorityActivity.class);
		startActivityForResult(intent, Constant.CaseChangePriority_CODE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			if (Constant.CaseChangePriority_CODE == requestCode) {
				if (resultCode == Constant.CaseChangePriority_CODE) {
					String Priority = data.getStringExtra(Constant.CaseChangePriority);
					if(Priority.equals("非常紧急")){
//						mTv_jump_priority.setBackgroundColor(Color.parseColor("#cb1a44"));
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority1));
					}else if(Priority.equals("紧急")){
//						mTv_jump_priority.setBackgroundColor(Color.parseColor("#ef5b1e"));
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority2));
					}else if(Priority.equals("重要")){
//						mTv_jump_priority.setBackgroundColor(Color.parseColor("#278785"));
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority3));
					}else if(Priority.equals("一般")){
//						mTv_jump_priority.setBackgroundColor(Color.parseColor("#59b2e0"));
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority4));
					}else if(Priority.equals("不重要")){
//						mTv_jump_priority.setBackgroundColor(Color.parseColor("#33a6b8"));
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority5));
					}else if(Priority.equals("不紧急不重要")){
//						mTv_jump_priority.setBackgroundColor(Color.parseColor("#9c90c2"));
						mTv_jump_priority.setBackgroundDrawable(getResources().getDrawable(R.drawable.priority6));
					}
					mTv_jump_priority.setText(Priority);
					
				}
			} else if (requestCode == Constant.CaseChangeStatus_CODE) {
				if (resultCode == Constant.CaseChangeStatus_CODE) {
					String Status = data.getStringExtra(Constant.CaseChangeStatus);
					mTv_jump_change_status.setText(Status);
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
